/*
 * @Author: Yeong Lee
 * @Date: 2021-02-15 01:28:54
 * @Last Modified by: Yeong Lee
 * @Last Modified time: 2021-03-03 23:08:17
 */
import {
  take, put, call, all,
} from 'redux-saga/effects';
import HomeActionTypes from '../actions/home';
import apiUrl from '../../components/common/apiUrl';
import request from '../../components/utils/request';
import {validRes} from '@utils/validator'

export function* getHomePageData() {
  while(true){
    try {
      yield take(HomeActionTypes.HOME_PAGE_DATA);
      console.info('请求首页数据');
      const payload = yield call(request, { url: apiUrl.HOME_PAGE_DATA, headers: { } } );
      const resData = validRes(payload);
      if (resData) {
        yield put({ type: HomeActionTypes.HOME_PAGE_DATA_UPDATE, payload:resData });
      }
      console.info('首页数据请求完成');
    } catch {
      console.error('首页数据请求失败');
    }
  }
}

export default function* homePageSaga() {
  const commons = [getHomePageData];
  yield all([...commons].map((saga) => call(saga)));
}
